package org.example.comment.config;


import org.example.comment.interception.UserInfoInterception;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
//@ConditionalOnClass(WebMvcConfigurer.class)     // 具有WebMvcConfigurer类才生效
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    private UserInfoInterception userInfoInterception;

    @Override
    public void addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry registry) {
        registry.addInterceptor(userInfoInterception);
    }



    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // 匹配所有路径
                .allowedOrigins("*")  // 允许所有来源（生产环境建议指定具体域名）
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法
                .allowedHeaders("*") // 允许所有请求头
                .allowCredentials(false) // 是否允许发送Cookie
                .maxAge(3600); // 预检请求缓存时间（秒）
    }

}
